package com.czl.module_activity.fragment

import android.annotation.SuppressLint
import android.text.TextUtils
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.DialogHelper
import com.czl.module_activity.BR
import com.czl.module_activity.R
import com.czl.module_activity.databinding.FragmentActivityDetailBinding
import com.czl.module_activity.view.ActivityDetailWebViewClient
import com.czl.module_activity.viewmodel.ActivityDetailViewModel

@Route(path = AppConstants.Router.Activities.F_ACTIVITIES_DETAIL)
class ActivityDetailFragment:BaseFragment<FragmentActivityDetailBinding,ActivityDetailViewModel>() {
    override fun initContentView(): Int {
        return R.layout.fragment_activity_detail
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun initData() {
        viewModel.activityId= arguments?.getString(AppConstants.BundleKey.ACTIVITY_ID).toString()
        var status=arguments?.getString(AppConstants.BundleKey.ACTIVITY_STATUS,"").toString()
        viewModel.fromPager=arguments?.getString(AppConstants.BundleKey.COMMUNITY_FROM_PAGER,"").toString()
        if (TextUtils.isEmpty(status)){
            viewModel.joinAble.set(true)
        }
        binding.wbRichtext.isHorizontalScrollBarEnabled=false
        binding.wbRichtext.isVerticalScrollBarEnabled=false
        binding.wbRichtext.settings.javaScriptEnabled=true
        binding.wbRichtext.webViewClient=ActivityDetailWebViewClient()
        viewModel.tvTitle.set("活动详情")
        reload()
    }

    override fun initViewObservable() {
        viewModel.onRefreshFinishEvent.observe(this,{
            binding.smartCommon.finishRefresh(it)
            if (!it){
                showErrorStatePage()
            }else{
                showSuccessStatePage()
            }
        })
        viewModel.onJoinActivityEvent.observe(this,{
            back()
        })
        viewModel.toJoinEvent.observe(this,{
            DialogHelper.showBaseDialog(requireContext(), "", "确定报名该活动吗？") {
                viewModel.joinActivity()
            }
        })
    }
    override fun reload() {
        super.reload()
        binding.smartCommon.autoRefresh()
    }

    override fun onDetach() {
        super.onDetach()
        LiveBusCenter.postCommunityRefreshEvent("")
    }

}